﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace dPet.Domain.Models
{
	using Hangerd.Entity;

	public class ServiceOrder : OrderBase
	{
		#region Public Properties

		/// <summary>
		/// 会员等级
		/// </summary>
		[RecordModify]
		public MembershipLevel MembershipLevel { get; private set; }

		/// <summary>
		/// 订单行
		/// </summary>
		public virtual ICollection<ServiceOrderLine> OrderLines { get; private set; }

		#endregion

		#region Constructors

		private ServiceOrder() { }

		public ServiceOrder(Hospital hospital, MembershipLevel membershipLevel, string @operator)
			: base(hospital, @operator)
		{
			this.OrderLines = new List<ServiceOrderLine>();
			this.MembershipLevel = membershipLevel;
		}

		#endregion

		#region Protected Methods

		protected override string GenerateOrderNumber(Hospital hospital)
		{
			return string.Format("SIO-{0}-{1}", hospital.Id.Substring(0, 6).ToUpper(), DateTime.Now.ToString("yyyyMMddHHmmss"));
		}

		#endregion

		#region Public Methods

		/// <summary>
		/// 添加订单行
		/// </summary>
		public void AddOrderLine(ServiceOrderLine orderLine)
		{
			this.OrderLines.Add(orderLine);
		}

		/// <summary>
		/// 完成订单
		/// </summary>
		public void Complete(PaymentMethod paymentMethod, string @operator)
		{
			base.InternalComplete(paymentMethod, @operator);
		}

		/// <summary>
		/// 取消订单
		/// </summary>
		public void Cancel(string @operator)
		{
			base.InternalCancel(@operator);
		}

		public override void CalculateTotalAmount()
		{
			this.TotalAmount = this.OrderLines.Sum(ol => ol.UnitPrice * ol.Quantity);
		}

		#endregion

		#region Private Methods

		#endregion
	}
}
